package com.ihg.hiex.admin.web.controller.session.during;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.ihg.hiex.admin.service.account.DelegateService;
import com.ihg.hiex.admin.service.session.SessionDuringService;
import com.ihg.hiex.admin.service.session.SessionMeetingService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.web.view.ExcelTemplateView;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.during.SessionAnchorScore;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/sessions/{sessionId}/mid-day-calibration")
@MenuSetting("menu-sessions")
public class MidDayCalibrationController {
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private SessionMeetingService sessionMeetingService;
    
    @Autowired
    private DelegateService delegateService;
    
    @Autowired
    private SessionDuringService sessionDuringService;
    
    /**
     * Day One Midday Calibration
     * 
     * @param sessionId
     * @return
     */
    @MenuSetting(value = "submenu-session-during-day-one")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public ModelAndView dayOneList(@PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/during/mid-day-calibration/mid-day-calibration-admin-list");

        // 1. load session
        Session session = sessionService.load(Session.class, sessionId);
        modelAndView.addObject("session", session);
        
        // 2. load meeting
        SessionMeeting meeting = sessionMeetingService.load(session, "Midday Calibration");
        modelAndView.addObject("meeting", meeting);
        
        // 3. list assessor
        List<Assessor> assessors = sessionService.listAssessor(session);
        modelAndView.addObject("assessors", assessors);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param sessionId
     * @param locale
     * @return
     */
    @RequestMapping(value = "/score-export", method = RequestMethod.GET)
    public ModelAndView onExport(@PathVariable("sessionId") Long sessionId, @RequestParam("locale") String locale) {
        
        ModelAndView modelAndView = new ModelAndView();
        
        if(StringUtils.equals(locale, "en_US")) {
            modelAndView.setView(new ExcelTemplateView("delegate-score-en.xls", "delegate-score.xls"));
        } else {
            modelAndView.setView(new ExcelTemplateView("delegate-score-cn.xls", "delegate-score.xls"));
        }
        
        Session session = sessionService.load(sessionId);
        
        // list delegates
        List<Delegate> delegates = delegateService.list(session);
        modelAndView.addObject("delegates", delegates);
        
        // list guest meeting anchorScore
        Map<String, Map<String, SessionAnchorScore>> guestMeetingAnchorScores = sessionDuringService.listMeetingAnchorScore(session, "Guest Meeting");
        modelAndView.addObject("guestMeetingAnchorScores", guestMeetingAnchorScores);
        
        // list colleague meeting anchorScore
        Map<String, Map<String, SessionAnchorScore>> colleagueMeetingAnchorScores = sessionDuringService.listMeetingAnchorScore(session, "Colleague Meeting");
        modelAndView.addObject("colleagueMeetingAnchorScores", colleagueMeetingAnchorScores);
        
        // list gm meeting anchorScore
        Map<String, Map<String, SessionAnchorScore>> gmMeetingAnchorScores = sessionDuringService.listMeetingAnchorScore(session, "GM Meeting");
        modelAndView.addObject("gmMeetingAnchorScores", gmMeetingAnchorScores);
        
        // list in-basket anchorScore
        Map<String, Map<String, SessionAnchorScore>> inBasketAnchorScores = sessionDuringService.listInBasketAnchorScore(session);
        modelAndView.addObject("inBasketAnchorScores", inBasketAnchorScores);
        
        return modelAndView;
        
    }
    
}
